<template>
  <div class="register-container">
    <div class="register-form">
      <div class="register-brand">
        <div class="logo-container">
          <h1 class="logo">Noody</h1>
          <img src="../assets/noody-logo.svg" alt="Noody Logo" class="logo-image" />
        </div>
      </div>
      <div class="register-header">
        <h1>注册</h1>
        <p>创建您的新账户</p>
      </div>
      
      <form @submit.prevent="handleRegister">
        <div class="form-group">
          <label for="username">用户名</label>
          <input 
            type="text" 
            id="username" 
            v-model="username" 
            placeholder="请设置用户名"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="nickname">昵称</label>
          <input 
            type="text" 
            id="nickname" 
            v-model="nickname" 
            placeholder="请设置昵称"
            required
          />
        </div>
        
        <div class="form-group">
          <label for="password">密码</label>
          <input 
            type="password" 
            id="password" 
            v-model="password" 
            placeholder="请设置密码"
            required
          />
          <!-- 密码要求提示 -->
          <div v-if="password.length > 0" class="password-requirements">
            <p v-if="!passwordValidations.hasMinLength" class="requirement invalid">密码长度至少为8位</p>
            <p v-if="!passwordValidations.hasLetter" class="requirement invalid">密码必须包含至少一个字母</p>
          </div>
          <!-- 密码强度指示器 -->
          <div v-if="password.length > 0" class="password-strength">
            <div class="strength-bars">
              <div class="strength-bar" :class="{ 'weak': passwordStrength >= 1, 'medium': passwordStrength >= 2, 'strong': passwordStrength >= 3 }"></div>
              <div class="strength-bar" :class="{ 'medium': passwordStrength >= 2, 'strong': passwordStrength >= 3 }"></div>
              <div class="strength-bar" :class="{ 'strong': passwordStrength >= 3 }"></div>
            </div>
            <span class="strength-text">
              {{ passwordStrength === 0 ? '未输入' : 
                 passwordStrength === 1 ? '弱' : 
                 passwordStrength === 2 ? '中' : '强' }}
            </span>
          </div>
        </div>
        
        <div class="form-group">
          <label for="confirmPassword">确认密码</label>
          <input 
            type="password" 
            id="confirmPassword" 
            v-model="confirmPassword" 
            placeholder="请再次输入密码"
            required
          />
          <p v-if="confirmPassword.length > 0 && password !== confirmPassword" class="requirement invalid">两次输入的密码不一致</p>
        </div>
        
        <div class="form-group agreement">
          <label class="agreement-label">
            <input type="checkbox" v-model="agreement" required>
            我已阅读并同意 <a href="#">用户协议</a> 和 <a href="#">隐私政策</a>
          </label>
        </div>
        
        <button type="submit" class="register-button">注册</button>
        
        <div class="login-link">
          <p>已有账户？<router-link to="/">返回登录</router-link></p>
        </div>
      </form>
      
      <!-- 错误提示 -->
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '../store/user'

const router = useRouter()
const userStore = useUserStore()

const username = ref('')
const nickname = ref('')
const password = ref('')
const confirmPassword = ref('')
const agreement = ref(false)
const error = ref('')
const passwordStrength = ref(0)

// 密码验证状态
const passwordValidations = reactive({
  hasMinLength: false,
  hasLetter: false
})

// 监听密码变化，更新验证状态和强度
watch(password, (newPassword) => {
  // 验证密码长度（至少8位）
  passwordValidations.hasMinLength = newPassword.length >= 8
  
  // 验证是否包含字母
  passwordValidations.hasLetter = /[a-zA-Z]/.test(newPassword)
  
  // 计算密码强度
  let strength = 0
  
  // 基础分：长度达标
  if (newPassword.length >= 8) {
    strength += 1
  }
  
  // 加分项：同时包含大小写字母
  if (/[a-z]/.test(newPassword) && /[A-Z]/.test(newPassword)) {
    strength += 1
  }
  
  // 加分项：包含特殊符号
  if (/[^a-zA-Z0-9]/.test(newPassword)) {
    strength += 1
  }
  
  passwordStrength.value = strength
})

// 处理注册逻辑
const handleRegister = () => {
  // 清除之前的错误
  error.value = ''
  
  // 验证密码长度
  if (!passwordValidations.hasMinLength) {
    error.value = '密码长度至少为8位'
    return
  }
  
  // 验证是否包含字母
  if (!passwordValidations.hasLetter) {
    error.value = '密码必须包含至少一个字母'
    return
  }
  
  // 验证密码是否一致
  if (password.value !== confirmPassword.value) {
    error.value = '两次输入的密码不一致'
    return
  }
  
  const success = userStore.register(username.value, password.value, nickname.value)
  if (success) {
    router.push('/')
  } else {
    error.value = userStore.error
  }
}
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  width: 100%;
  background: linear-gradient(135deg, #e8f5e9 0%, #4CAF50 100%);
  margin: 0;
  padding: 0;
}

.register-form {
  background: white;
  border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 500px;
  min-width: 300px;
}

.register-brand {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  padding-top: 20px;
}

.logo-container {
  display: flex;
  align-items: center;
}

.logo {
  color: #4CAF50;
  font-weight: 700;
  margin: 0;
  font-size: 28px;
}

.logo-image {
  margin-left: 10px;
  width: 35px;
  height: 35px;
  vertical-align: middle;
}

.register-header {
  text-align: center;
  margin-bottom: 30px;
}

.register-header h1 {
  margin: 0;
  font-size: 28px;
  color: #333;
}

.register-header p {
  margin: 10px 0 0;
  color: #666;
}

.form-group {
  margin-bottom: 20px;
}

.form-group label {
  display: block;
  margin-bottom: 8px;
  font-weight: 500;
  color: #333;
}

.form-group input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 5px;
  font-size: 16px;
  transition: border-color 0.3s;
}

/* 密码要求样式 */
.password-requirements {
  margin-top: 8px;
}

.requirement {
  margin: 4px 0;
  font-size: 12px;
  line-height: 1.4;
}

.requirement.valid {
  color: #4CAF50;
}

.requirement.invalid {
  color: #f44336;
}

/* 密码强度指示器样式 */
.password-strength {
  margin-top: 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.strength-bars {
  display: flex;
  gap: 4px;
  flex: 1;
  max-width: 150px;
}

.strength-bar {
  height: 6px;
  flex: 1;
  background-color: #e0e0e0;
  border-radius: 3px;
  transition: background-color 0.3s;
}

.strength-bar.weak {
  background-color: #f44336;
}

.strength-bar.medium {
  background-color: #ff9800;
}

.strength-bar.strong {
  background-color: #4CAF50;
}

.strength-text {
  font-size: 12px;
  font-weight: 500;
  color: #666;
}

/* 密码强度文字颜色 */
.password-strength:has(.strength-bar.weak):not(.strength-bar.medium) .strength-text {
  color: #f44336;
}

.password-strength:has(.strength-bar.medium):not(.strength-bar.strong) .strength-text {
  color: #ff9800;
}

.password-strength:has(.strength-bar.strong) .strength-text {
  color: #4CAF50;
}

.form-group input:focus {
  outline: none;
  border-color: #667eea;
}

.agreement {
  margin-bottom: 25px;
}

.agreement-label {
  display: flex;
  align-items: flex-start;
  cursor: pointer;
  font-weight: normal;
  color: #666;
  font-size: 14px;
}

.agreement-label input {
  margin-right: 8px;
  margin-top: 3px;
  width: auto;
}

.agreement-label a {
  color: #667eea;
  text-decoration: none;
}

.agreement-label a:hover {
  text-decoration: underline;
}

.register-button {
  width: 100%;
  padding: 12px;
  background-color: #4CAF50;
  color: white;
  border: none;
  border-radius: 5px;
  font-size: 16px;
  font-weight: bold;
  cursor: pointer;
  transition: background-color 0.3s;
}

.register-button:hover {
  background-color: #45a049;
}

.login-link {
  text-align: center;
  margin-top: 20px;
}

.login-link a {
  color: #667eea;
  text-decoration: none;
  font-weight: 500;
  transition: color 0.3s;
}

.login-link a:hover {
  color: #764ba2;
  text-decoration: underline;
}

.error-message {
  margin-top: 15px;
  padding: 10px;
  background: #f8d7da;
  color: #721c24;
  border: 1px solid #f5c6cb;
  border-radius: 5px;
  text-align: center;
}
</style>